home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / ProgressBars 1.0 / Sources / Menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  4.7 KB  |  269 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Menus.c
  3.  
  4.     Contains:    Handles the application's menus
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.             1/22/96            CW        First release
  13.  
  14. */
  15.  
  16.  
  17. #pragma segment Core
  18.  
  19.  
  20. #ifndef __MENUS__
  21.     #include <Menus.h>
  22. #endif
  23.  
  24. #ifndef __WINDOWS__
  25.     #include <Windows.h>
  26. #endif
  27.  
  28. #ifndef __DIALOGS__
  29.     #include <Dialogs.h>
  30. #endif
  31.  
  32. #ifndef __TEXTUTILS__
  33.     #include <TextUtils.h>
  34. #endif
  35.  
  36. #ifndef __DESK__
  37.     #include <Desk.h>
  38. #endif
  39.  
  40.  
  41.  
  42.  
  43.  
  44. // Application includes
  45.  
  46. #ifndef __BAREBONES__
  47.     #include "BareBones.h"
  48. #endif
  49.  
  50. #ifndef __PROTOTYPES__
  51.     #include "Prototypes.h"
  52. #endif
  53.  
  54.  
  55.  
  56.  
  57.  
  58. // static includes
  59.  
  60. static void        DoAppleCmds ( SInt16 theMenu, SInt16 theItem );
  61. static void        DoFileCmds ( SInt16 theMenu, SInt16 theItem );
  62. static void        DoProgressBarsCmds ( SInt16 theMenu, SInt16 theItem );
  63. static Boolean    EnableFileCmds ( WindowRef frontWindow );
  64. static Boolean    EnableProgressBarsCmds ( WindowRef frontWindow );
  65.  
  66. static void        SetItemEnable ( MenuHandle theMenu, SInt16 theItem, Boolean bEnabled );
  67.  
  68.  
  69.  
  70.  
  71.  
  72. void MenuDispatch ( SInt32 menuResult )
  73. {
  74.     SInt16     theMenu = (menuResult >> 16);                    // menu selected
  75.     SInt16     theItem = (menuResult & 0x0000FFFF);               // item selected
  76.     
  77.     switch (theMenu)
  78.     {
  79.         case kAppleMenu: 
  80.             DoAppleCmds ( theMenu, theItem );
  81.         break;
  82.         
  83.         case kFileMenu: 
  84.             DoFileCmds ( theMenu, theItem );
  85.         break;
  86.         
  87.         case kProgressBarsMenu:
  88.             DoProgressBarsCmds ( theMenu, theItem );
  89.         break;
  90.         
  91.     }
  92.     
  93.     HiliteMenu ( 0 );               // un-hilite selected menu
  94.     
  95.     return;
  96.     
  97. } // MenuDispatch
  98.  
  99.  
  100.  
  101. void AdjustMenus ( void )
  102. {
  103.     Boolean        bRedraw = false;
  104.     WindowRef    frontWindow;
  105.     
  106.     frontWindow = FrontWindow ( );
  107.     if ( EnableFileCmds ( frontWindow ) )
  108.         bRedraw = true;
  109.     if ( EnableProgressBarsCmds ( frontWindow ) )
  110.         bRedraw = true;
  111.     
  112.     if ( bRedraw )
  113.         DrawMenuBar ( );
  114.     
  115.     return;
  116.     
  117. } // AdjustMenus
  118.  
  119.  
  120.  
  121. static void DoAppleCmds ( SInt16 theMenu, SInt16 theItem )
  122. {
  123.     Str255    name;            // string for DA name
  124.     
  125.     
  126.     switch ( theItem )
  127.     {
  128.         case cAbout:
  129.             Alert ( kAboutDialog, nil );
  130.         break;
  131.         
  132.         default:
  133.             GetMenuItemText ( GetMenuHandle ( kAppleMenu ), theItem, (StringPtr) &name );
  134.             OpenDeskAcc ( (StringPtr) &name );
  135.         break;
  136.     }
  137.     
  138.     return;
  139.     
  140. } // DoAppleCmds
  141.  
  142.  
  143.  
  144. static void DoFileCmds ( SInt16 theMenu, SInt16 theItem )
  145. {    
  146.     switch ( theItem )
  147.     {
  148.         case cQuit:
  149.             gQuit = true;
  150.         break;
  151.     }
  152.     
  153.     return;
  154.     
  155. } // DoFileCmds
  156.  
  157.  
  158.  
  159. static void DoProgressBarsCmds ( SInt16 theMenu, SInt16 theItem )
  160. {    
  161.     OSErr            theErr = noErr;
  162.     Str255            theText;
  163.     static Boolean    bUseThreads = false;
  164.     
  165.     
  166.     switch ( theItem )
  167.     {
  168.         case cToggleThreads:
  169.             bUseThreads = !bUseThreads;
  170.             CheckItem ( GetMenuHandle ( theMenu ), theItem, bUseThreads );
  171.         break;
  172.         
  173.         case cStandard:
  174.             GetIndString ( theText, kMiscStrings, kProgressStr );
  175.             if ( bUseThreads )
  176.                 theErr = ThreadedProgressOperation ( ThreadedStandardDemoOperation, nil,
  177.                                                                     theText, nil, false );
  178.             else
  179.                 theErr = ProgressOperation ( StandardDemoOperation, nil, theText );
  180.         break;
  181.         
  182.         case cBarberPole:
  183.             GetIndString ( theText, kMiscStrings, kProgressStr );
  184.             if ( bUseThreads )
  185.                 theErr = ThreadedProgressOperation ( ThreadedBarberPoleDemoOperation, nil,
  186.                                                                     theText, nil, true );
  187.             else
  188.                 theErr = ProgressOperation ( BarberPoleDemoOperation, nil, theText );
  189.         break;
  190.     }
  191.     
  192.     if ( theErr )
  193.         AlertUser ( kGenericErrorStr, theErr, "\p" );
  194.     
  195.     return;
  196.     
  197. } // DoProgressBarsCmds
  198.  
  199.  
  200.  
  201. static Boolean EnableFileCmds ( WindowRef frontWindow )
  202. {
  203.     static Boolean    bIsEnabled = true;
  204.     Boolean            bHasDialog = false;
  205.     Boolean            bUpdate = false;
  206.     MenuHandle        theMenu = GetMenuHandle ( kFileMenu );
  207.     
  208.     
  209.     bHasDialog = frontWindow && GetWindowKind ( frontWindow ) == dialogKind;
  210.     
  211.     if ( bIsEnabled == bHasDialog )        // Or bIsEnabled != !bHasDialog
  212.     {
  213.         SetItemEnable ( theMenu, 0, !bHasDialog );
  214.         bIsEnabled = !bHasDialog;
  215.         bUpdate = true;
  216.     }
  217.     
  218.     SetItemEnable ( theMenu, cQuit, !bHasDialog );
  219.     
  220.     return bUpdate;
  221.     
  222. } // EnableFileCmds
  223.  
  224.  
  225.  
  226. static Boolean EnableProgressBarsCmds ( WindowRef frontWindow )
  227. {
  228.     static Boolean    bIsEnabled = true;
  229.     Boolean            bHasDialog = false;
  230.     Boolean            bUpdate = false;
  231.     MenuHandle        theMenu = GetMenuHandle ( kProgressBarsMenu );
  232.     
  233.     
  234.     bHasDialog = frontWindow && GetWindowKind ( frontWindow ) == dialogKind;
  235.     
  236.     if ( bIsEnabled == bHasDialog )        // Or bIsEnabled != !bHasDialog
  237.     {
  238.         SetItemEnable ( theMenu, 0, !bHasDialog );
  239.         bIsEnabled = !bHasDialog;
  240.         bUpdate = true;
  241.     }
  242.     
  243.     SetItemEnable ( theMenu, cToggleThreads, gHasThreadManager );
  244.     
  245.     return bUpdate;
  246.     
  247. } // EnableProgressBarsCmds
  248.  
  249.  
  250.  
  251. static void SetItemEnable ( MenuHandle theMenu, SInt16 theItem, Boolean bEnabled )
  252. {
  253.     if ( theMenu )
  254.     {
  255.         if ( bEnabled )
  256.             EnableItem ( theMenu, theItem );
  257.         else
  258.             DisableItem ( theMenu, theItem );
  259.     }
  260.         
  261.     return;
  262.     
  263. } // SetItemEnable
  264.  
  265.  
  266.  
  267.  
  268.  
  269.